describe("Renoise Web Control", function(){

	describe("When connected to renoise", function(){
		it("makes RNS object available", function(){
			expect(rns).to.exist
		})
	})
	
	describe("When sending basic arithmetics to Renoise", function(){
		var response
		
		before(function(done){
			sendMessageAndWait("return 1 + 2", function(){
				response = rns.capturedCalls.pop()
				done()
			})
		})
		
		it("receives a correct reply", function(){
			expect(response).to.equal(3)
		})
	})
	
	describe("When sending 20 000 character message to be echoed in Renoise", function(){
		var response
		var message = ""
		
		before(function(done){
			
			for(var i = 0; i < 20000; i++){ message += i % 9 }
		
			sendMessageAndWait('return "' + message + '"', function(){
				response = rns.capturedCalls.pop()
				done()
			})
		})
		
		it("receives the exact message back", function(){
			expect(response).to.equal(message)
		})
	})
	
	describe("When setting up an artist observer, and modifying artist", function(){
		var response
		
		before(function(done){
			rns.observe('song().artist', 'artist')
			sendMessageAndWait('song().artist = "Test Artist"', function(){
				response = rns.capturedCalls.pop()
				done()
			})
		})
		
		it('should receive a correct artist message', function(){
			expect(response.artist).to.equal('Test Artist')
		})
		
		describe('and when removing artist observer, changing the artist', function(){
			var numberOfCapturedCalls
			
			before(function(done){
				numberOfCapturedCalls = rns.capturedCalls.length
				rns.unobserve('song().artist')
				rns.run('song().artist = ""')
				setTimeout(done, 100) // Wait a little to be sure the message is sent
			})
			
			it('does not receive any new messages from Renoise', function(){
				expect(rns.capturedCalls.length).to.equal(numberOfCapturedCalls)
			})
		})
	})
	
	describe("When setting up tracks observer, and swapping tracks", function(){
		var response
		
		before(function(done){
			rns.observe('song().tracks', 'tracks')
			sendMessageAndWait('song():swap_tracks_at(1, 2)', function(){
				response = rns.capturedCalls.pop()
				done()
			})
		})
		
		after(function(){
			rns.unobserve('song().tracks')
		})
		
		it('receives a message about track changes', function(){
			expect(response).to.have.property('tracks')
		})
		
		it('contains event property with correct event description', function(){
			expect(response).to.have.property('event')
			expect(response.event.index1).to.equal(1)
			expect(response.event.index2).to.equal(2)
			expect(response.event.type).to.equal('swap')
		})
	})

	function sendMessageAndWait(message, done) {
		rns.responseReceived = done
		rns.run(message)
	}
})
